/*
https://leetcode.cn/problems/fruit-into-baskets/submissions/562215896/
LC904. 水果成篮
*/
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int count=0;int l=0;
        unordered_map<int,int>mp;
        for(int i=0;i<fruits.size();++i){
            ++mp[fruits[i]];
            while(mp.size()>2){
                auto it=mp.find(fruits[l]);
                --it->second;
                if(it->second==0) mp.erase(it);
                ++l;
            }
            count=max(count,i-l+1);
        }
        return count;
    }
};